﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.ViewModel;
using System.Collections.ObjectModel;
using Microsoft.Practices.Prism.Modularity;
using System.Collections.Generic;
using System.ComponentModel;
using HSE.Common;

namespace HSE
{
    public class XtszViewModel : NotificationObject
    {
        private ObservableCollection<ToolBarNaviInfo> _functions;
        public ObservableCollection<ToolBarNaviInfo> Functions
        {
            get
            {
                return this._functions;
            }

            set
            {

                this._functions = value;
                this.RaisePropertyChanged("Functions");
            }
        }

        private List<ToolBarNaviInfo> masterList = new List<ToolBarNaviInfo>()
			{
                new ToolBarNaviInfo() {IsLiked = true, Order = 10, Name = "首页", Icon = "Images/navi/dashboard.png", Ref = "HSE.xap", InitializationMode = InitializationMode.OnDemand, ModuleName = "DefaultModule", ViewName = "Default" },
				new ToolBarNaviInfo() {IsLiked = false,Order = 11, Name = "文化引领", Icon = "Images/navi/posts.png", Ref = "HSE.xap", InitializationMode = InitializationMode.OnDemand, ModuleName = "WhylModule", ViewName = "Whyl" },
		    	new ToolBarNaviInfo() {IsLiked = true, Order = 12, Name = "责任落实", Icon = "Images/navi/media.png", Ref = "HSE.xap", InitializationMode = InitializationMode.OnDemand, ModuleName = "ZrlsModule", ViewName = "Zrls" },
                new ToolBarNaviInfo() {IsLiked = false,Order = 13, Name = "培训管理", Icon = "Images/navi/notes.png", Ref = "HSE.xap", InitializationMode = InitializationMode.OnDemand, ModuleName = "PxglModule", ViewName = "Pxgl" },
                new ToolBarNaviInfo() {IsLiked = false,Order = 14, Name = "管理机制", Icon = "Images/navi/gljz.png", Ref = "HSE.xap", InitializationMode = InitializationMode.OnDemand, ModuleName = "GljzModule", ViewName = "Gljz" },
                new ToolBarNaviInfo() {IsLiked = true, Order = 15, Name = "装备保障", Icon = "Images/navi/coin.png", Ref = "HSE.xap", InitializationMode = InitializationMode.OnDemand, ModuleName = "ZbbzModule", ViewName = "Zbbz" },
                new ToolBarNaviInfo() {IsLiked = false,Order = 16, Name = "风险管理", Icon = "Images/navi/users.png", Ref = "HSE.xap", InitializationMode = InitializationMode.OnDemand, ModuleName = "FxglModule", ViewName = "Fxgl" },
                new ToolBarNaviInfo() {IsLiked = true, Order = 17, Name = "整改落实", Icon = "Images/navi/zgls1.png", Ref = "HSE.xap", InitializationMode = InitializationMode.OnDemand, ModuleName = "ZglsModule", ViewName = "ZglsModule" },
                new ToolBarNaviInfo() {IsLiked = false,Order = 18, Name = "统计报表", Icon = "Images/navi/chart.png", Ref = "HSE.xap", InitializationMode = InitializationMode.OnDemand, ModuleName = "TjbbModule", ViewName = "Tjbb" },
                new ToolBarNaviInfo() {IsLiked = true, Order = 19, Name = "任务管理", Icon = "Images/navi/task.png", Ref = "HSE.xap", InitializationMode = InitializationMode.OnDemand, ModuleName = "RwglModule", ViewName = "Rwgl" },
                new ToolBarNaviInfo() {IsLiked = false,Order = 20, Name = "系统设置", Icon = "Images/navi/sys.png", Ref = "HSE.xap", InitializationMode = InitializationMode.OnDemand, ModuleName = "XtszModule", ViewName = "Xtsz" }
            };

        private ObservableCollection<ToolBarNaviInfo> roots = new ObservableCollection<ToolBarNaviInfo>();
        private ObservableCollection<ToolBarNaviInfo> ownroots = new ObservableCollection<ToolBarNaviInfo>();
        private double numberOfItems;

        public ObservableCollection<ToolBarNaviInfo> Roots { get { return this.roots; } }
        public ObservableCollection<ToolBarNaviInfo> OwnRoots { get { return this.ownroots; } }
        public double MaxNumberOfItems { get { return this.masterList.Count; } }
        public double NumberOfItems
        {
            get { return this.numberOfItems; }
            set { this.numberOfItems = value; this.RaisePropertyChanged("NumberOfItems"); this.UpdateCollections(); }
        }

        public XtszViewModel()
        {

            //ObservableCollection<SysModuleViewModel> tempsf = new ObservableCollection<SysModuleViewModel>();
            //tempsf.Add(new Function() { Notice = "用户管理", Image = "../Images/dashboard/yg.png", Newnum = 3 });
            //tempsf.Add(new Function() { Notice = "部门管理", Image = "../Images/dashboard/xmb.png", Newnum = 8 });
            //tempsf.Add(new Function() { Notice = "岗位管理", Image = "../Images/dashboard/gwgl.png", Newnum = 0 });
            //tempsf.Add(new Function() { Notice = "权限设置", Image = "../Images/dashboard/qxgl.png", Newnum = 3 });
            //tempsf.Add(new Function() { Notice = "设备管理", Image = "../Images/dashboard/sbgl.png", Newnum = 0 });

            //tempsf.Add(new Function() { Notice = "责任区管理", Image = "../Images/dashboard/zrq.png", Newnum = 8 });
            //tempsf.Add(new Function() { Notice = "标准管理", Image = "../Images/dashboard/bzgl.png", Newnum = 0 });
            //tempsf.Add(new Function() { Notice = "资料管理", Image = "../Images/dashboard/zlxz.png", Newnum = 3 });
            //tempsf.Add(new Function() { Notice = "初始化配置", Image = "../Images/dashboard/csh.png", Newnum = 0 });


            //this.Functions = tempsf;

            //this.numberOfItems = this.masterList.Count;

            //foreach (ToolBarNaviInfo item in this.masterList)
            //{
            //    item.PropertyChanged += OnItemPropertyChanged;
            //}

            //this.UpdateCollections();
        }

        private void UpdateCollections()
        {
            this.UpdateObservableCollection(this.roots, true);
            this.UpdateObservableCollection(this.ownroots, false);
        }

        private void UpdateObservableCollection(ObservableCollection<ToolBarNaviInfo> collection, bool filter)
        {
            int index = 0;
            int collectionIndex = 0;

            foreach (ToolBarNaviInfo item in this.masterList)
            {
                bool itemIsAvailable = collectionIndex < this.NumberOfItems;

                if (item.IsLiked == filter && itemIsAvailable)
                {
                    if (index >= collection.Count || collection[index] != item)
                    {
                        if (collection.Contains(item))
                        {
                            collection.Remove(item); // don't have it in place twice
                        }
                        collection.Insert(index, item);
                    }
                    index++;
                }
                else
                {
                    if (index < collection.Count && collection[index] == item)
                    {
                        collection.RemoveAt(index);
                    }
                }

                collectionIndex++;
            }

            while (collection.Count > index)
            {
                collection.RemoveAt(index);
            }
        }

        private void OnItemPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            this.UpdateCollections();
        }

        public void SortOriginal()
        {
            this.masterList.Sort(new Comparison<ToolBarNaviInfo>(delegate(ToolBarNaviInfo a, ToolBarNaviInfo b) { return (int)(a.Order - b.Order); }));
            this.UpdateCollections();
        }

        public void SortAlphabetical()
        {
            this.masterList.Sort(new Comparison<ToolBarNaviInfo>(delegate(ToolBarNaviInfo a, ToolBarNaviInfo b) { return String.Compare(a.Name, b.Name); }));
            this.UpdateCollections();
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
